﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRepository;
using MvcUI.Models;
using Entities;
namespace MvcUI.Controllers
{
    public class MUserController : BaseController
    {

       private IUserRepository repository;
        public MUserController(IRepository.IUserRepository userRepository)
        {
            repository = userRepository;
        }

        [HttpGet]
        public ActionResult List(int index = 1)
        {
            ListUserViewModel vm = new ListUserViewModel
            {
                Users = repository.GetList("1=1")
                    .OrderBy(p => p.UserName)
                    .Skip((index - 1) * pageSize)
                    .Take(pageSize),
                PagingInfo = new PagingInfo
                {
                    TotalItems = repository.GetRecordCount("1=1"),
                    CurrentPage = index,
                    ItemsPerPage = pageSize,
                    OrderField = "UserName",
                    OrderDirection = "desc",
                }
                
            };
            return View(vm);
           
        }
        [HttpPost]
        public ActionResult List(FormCollection formCollectin)
        {
            int pageIndex = Request["pageNum"] == null ? 1 : Convert.ToInt32(Request["pageNum"]);
            pageSize = Request["numPerPage"] == null ? pageSize : Convert.ToInt32(Request["numPerPage"]);
            string orderField = Request["orderField"] == null ? "UserName" : Request["orderField"].ToString();
            string orderDirection = Request["orderDirection"] == null ? "asc" : Request["orderDirection"].ToString();
            string keywords = Request["keywords"] == null ? "" : Request["keywords"].ToString();
            string strWhere = String.IsNullOrEmpty(keywords) ? "1=1" : "UserName='" + keywords + "'";

            ListUserViewModel vm = new ListUserViewModel
            {
                Users = repository.GetListByPage(strWhere, orderField + " " + orderDirection, (pageIndex - 1) * pageSize + 1, pageIndex * pageSize).AsEnumerable(),
                KeyWord=keywords,
                PagingInfo = new PagingInfo
                {
                    TotalItems = repository.GetRecordCount(strWhere),
                    CurrentPage = pageIndex,
                    ItemsPerPage = pageSize,
                    OrderField = orderField,
                    OrderDirection = orderDirection,
                }

            };
            // return View("List", vm);
            return View(vm);
        }

        [HttpGet]
        public ActionResult Add()
        {
            
            return View();
        }
        [HttpPost]
        public string Add(Entities.User user)
        {
            user.ID = Guid.NewGuid().ToString();
            user.Creator = this.User.ID;
            user.CreateTime = DateTime.Now;
            user.Updator = this.User.ID;
            user.UpdateTime = DateTime.Now;
            user.Phone = String.IsNullOrEmpty(user.Phone) ? "" : user.Phone;
            user.Remark = String.IsNullOrEmpty(user.Remark) ? "" : user.Remark;
            bool result = repository.Add(user);
            if (result)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }


        [HttpGet]
        public ActionResult Edit( string id="-1")
        {
            Entities.User user = repository.Get(id);
            user.Password = "";
            return View(user);
        }

        [HttpPost]
        public string Edit(Entities.User user)
        {
            user.Updator = this.User.ID;
            user.UpdateTime = DateTime.Now;
            user.Phone = String.IsNullOrEmpty(user.Phone) ? "" : user.Phone;
            user.Remark = String.IsNullOrEmpty(user.Remark) ? "" : user.Remark;

            bool result = repository.Update(user);
            if (result)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }

        [HttpPost]
        public string Delete(string id = "-1")
        {
            bool result = repository.Delete(id.ToString());
            if (result)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }

        [HttpPost]
        public string DeleteBatch()
        {
            List<string> listID = Request.Params["ids"] == null ? null : Request.Params["ids"].Split(',').ToList();
            int result = 0;
            if (listID.Count > 0)
            {
                result = Convert.ToInt32(repository.Delete(listID));
            }

            if (result > 0)
            {
                return SetOperateResultNav("200", "操作成功", "", "", "", "", "");
            }
            else
            {
                return SetOperateResultNav("300", "操作失败", "", "", "", "", "");
            }
        }
    }
}
